﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Rose.Models;
using System.Text.RegularExpressions;

namespace Rose.Helper
{
    public class SearchHelpModel
    {
        private  Database_RoseEntities db = new Database_RoseEntities();

        private  RecommendModel ChangeUserInfoToRecommendModel(UserInfo recommend)
        {
            return new RecommendModel()
            {
                UserId = recommend.Id,
                TrueName = recommend.Name ?? string.Empty,
                ImagePath = recommend.ImagePath ?? string.Empty,
                Address = recommend.Address ?? string.Empty,
                University = recommend.University ?? string.Empty,
                MajorClass = recommend.CollegeYear ?? string.Empty,
                SchoolYear = recommend.SchoolYear.ToString(),
                Hobbies = new UserInfoHelpModel().GetHobbies(recommend.Id) ?? string.Empty,
                Gender = recommend.Gender,
                College=recommend.College
            };
        }

        private  List<RecommendModel> ChangeUserInfosToRecommendModels(IEnumerable<UserInfo> recommendList)
        {
            List<RecommendModel> returnModel = new List<RecommendModel>();
            foreach (UserInfo recommend in recommendList)
            {
                try
                {
                    returnModel.Add(ChangeUserInfoToRecommendModel(recommend));
                }
                catch
                {
                    continue;
                }
            }
            return returnModel;
        }

        private  List<UserInfo> GetUserInfo(string name,string university,bool isSameSchool)
        {
            if (!isSameSchool)
            {
                return db.UserInfos.Where(c => c.Name.Contains(name)).OrderBy(c => c.PhotoCount).ToList();
            }
            return db.UserInfos.Where(c => c.Name.Contains(name) && c.University == university).OrderBy(c => c.PhotoCount).ToList();

        }

        private List<UserInfo> GetUserInfoByCollege(string college,string university)
        {
            return db.UserInfos.Where(c => c.University == university && c.College == college).OrderBy(c => c.PhotoCount).ToList();
        }





        public  List<RecommendModel> GetSearchFriends(string name, string university, bool isSameSchool,int currentpage=1,int pagesize=10)
        {
            return ChangeUserInfosToRecommendModels(GetUserInfo(name,university , isSameSchool).Skip((currentpage-1)*pagesize).Take(pagesize));
        }

        public List<RecommendModel> GetCollegeFriends(string college,string university,int currentpage=1,int pagesize=10)
        {
            return ChangeUserInfosToRecommendModels(GetUserInfoByCollege(college,university).Skip((currentpage-1)*pagesize).Take(pagesize));
        }



        public int GetSearchFriendsCount(string name, string university, bool isSameSchool)
        {
            return GetUserInfo(name, university, isSameSchool).Count();
        }


        public int GetCollegeFriendsCount(string college,string university)
        {
           return  GetUserInfoByCollege(college,university).Count();
        }

    }
}